﻿#include "TownArcher.h"
#include "../../MirEnvir/Envir.h"
#include "../../../Shared/Enums.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../../Shared/ServerPackets.h"
#include "../../../Shared/Data/Stat.h"
#include "../../MirEnvir/Map.h"
#include "../MapObject.h"
#include "../PlayerObject.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    bool TownArcher::getCanMove() const
    {
        return Route.size() > 0 && !Dead && getEnvir()->getTime() > MoveTime && getEnvir()->getTime() > ActionTime && getEnvir()->getTime() > ShockTime;
    }

    TownArcher::TownArcher(MonsterInfo *info) : MonsterObject(info)
    {
    }

    bool TownArcher::IsAttackTarget(MonsterObject *attacker)
    {
        return false;
    }

    void TownArcher::Spawned()
    {
        if (Respawn != nullptr && Respawn->Info->Direction < 8)
        {
            setDirection(static_cast<MirDirection>(Respawn->Info->Direction));
        }

        MonsterObject::Spawned();
    }

    void TownArcher::Attack()
    {
        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        ShockTime = 0;

        setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));
        S::ObjectRangeAttack *tempVar = new S::ObjectRangeAttack();
        tempVar->ObjectID = ObjectID;
        tempVar->Direction = getDirection();
        tempVar->Location = getCurrentLocation();
        tempVar->TargetID = getTarget()->ObjectID;
        Broadcast(tempVar);

        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;

        ProjectileAttack(Stats[Stat::MinDC], Stats[Stat::MaxDC]);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
    }

    void TownArcher::ProcessTarget()
    {
        if (getTarget() == nullptr || !getCanAttack())
        {
            return;
        }

        if (InAttackRange() && getEnvir()->getTime() < FearTime)
        {
            Attack();
            return;
        }

        FearTime = getEnvir()->getTime() + 2000;

        if (getEnvir()->getTime() < ShockTime)
        {
            setTarget(nullptr);
            return;
        }

        int dist = Functions::MaxDistance(getCurrentLocation(), getTarget()->getCurrentLocation());

        if (dist > AttackRange) // || Target.PKPoints <= 99
        {
            setTarget(nullptr);

            if (Respawn != nullptr)
            {
                setDirection(static_cast<MirDirection>(Respawn->Info->Direction));
            }
        }
    }

    void TownArcher::FindTarget()
    {
        for (int d = 0; d <= Info->ViewRange; d++)
        {
            for (int y = getCurrentLocation()->Y - d; y <= getCurrentLocation()->Y + d; y++)
            {
                if (y < 0)
                {
                    continue;
                }
                if (y >= getCurrentMap()->Height)
                {
                    break;
                }

                for (int x = getCurrentLocation()->X - d; x <= getCurrentLocation()->X + d; x += std::abs(y - getCurrentLocation()->Y) == d ? 1 : d * 2)
                {
                    if (x < 0)
                    {
                        continue;
                    }
                    if (x >= getCurrentMap()->Width)
                    {
                        break;
                    }

                    Cell *cell = getCurrentMap()->GetCell(x, y);
                    if (!cell->getValid() || cell->Objects.empty())
                    {
                        continue;
                    }

                    for (int i = 0; i < cell->Objects.size(); i++)
                    {
                        MapObject *ob = cell->Objects[i];
                        switch (ob->getRace())
                        {
                            case ObjectType::Player:
                            {
                                PlayerObject *playerob = static_cast<PlayerObject*>(ob);
                                if (!ob->IsAttackTarget(this))
                                {
                                    continue;
                                }
                                if (playerob->getPKPoints() < 200 || ob->getHidden() && (!CoolEye || getLevel() < ob->getLevel()))
                                {
                                    continue;
                                }
                                setTarget(ob);
                                return;
                            }
                            default:
                                continue;
                        }
                    }
                }
            }
        }
    }

    bool TownArcher::InAttackRange()
    {
        return getCurrentMap() == getTarget()->getCurrentMap() && Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), AttackRange);
    }
}
